<?php


namespace app\service;


use app\Code;
use app\dao\RolesDao;
use app\dao\RouteRolesDao;
use app\dao\RoutesDao;
use app\dao\UserDao;
use app\lib\exception\ServiceException;
use think\facade\Db;

class RoutesService
{
    /**
     * @var RouteRolesDao
     */
    private $routesRolesDao;
    /**
     * @var RolesDao
     */
    private $rolesDao;
    /**
     * @var RoutesDao
     */
    private $routesDao;

    public function __construct()
    {
        $this->rolesDao = new RolesDao();
        $this->routesDao = new RoutesDao();
        $this->routesRolesDao = new RouteRolesDao();
    }

    public function addRoute($url, $method, $roles)
    {
        $result = [
            'route'=> [],
            'roles' => []
        ];

        Db::startTrans();//开启事物
        try {
            $result['route'] = $this->routesDao->save([
                'url' => $url,
                'method' => $method
            ]);

            if (!empty($roles)){
                $roles = explode(',', $roles);
                foreach ($roles as $role){
                    $roleInfo = $this->rolesDao->find($role);

                    if ($roleInfo != null){
                        $this->routesRolesDao->save([
                            'route_id' => $result['route']->id,
                            'role_id' => $role,
                        ]);
                        $result['roles'][] = $roleInfo->name;
                    }
                }
            }


            Db::commit();//提交事物
        }catch (\Exception $exception){
            Db::rollback();//回滚事物
            throw new ServiceException($exception->getMessage());
        }

        return $result;
    }

    public function editRoute($id, $url, $roles)
    {
        Db::startTrans();//开启事物
        try {
            $routes = $this->routesRolesDao->customQuery()->filterByRouteId($id)->getAll(true);

            if ($routes == null)throw new ServiceException('无此数据', Code::PARAM_EXCEPTION);

            if (!empty($roles)){

                array_map(function ($route){
                    $this->routesRolesDao->delete($route['id']);
                },$routes);

                $roles = explode(',', $roles);
                foreach ($roles as $role){
                    $roleInfo = $this->rolesDao->find($role);

                    if ($roleInfo != null){
                        $this->routesRolesDao->save([
                            'route_id' => $id,
                            'role_id' => $role,
                        ]);
                        $result['roles'][] = $roleInfo->name;
                    }
                }
            }

            if (!empty($url)) $this->routesDao->edit($id,[
                'url' => $url
            ]);

            Db::commit();//提交事物
        }catch (\Exception $exception){
            Db::rollback();//回滚事物
            throw new ServiceException($exception->getMessage());
        }
    }

    public function getRoute($keyword, $page, $limit)
    {
        $list = $this->routesDao->customQuery()
            ->fuzzyByUrl($keyword)
            ->withRole()
            ->order('id', false)
            ->getList($page, $limit);

        return $list;
    }

    public function delRoute($id)
    {
        Db::startTrans();//开启事物
        try {
            $routes = $this->routesRolesDao->customQuery()->filterByRouteId($id)->getAll(true);

            if ($routes != null){
                array_map(function ($route){
                    $this->routesRolesDao->delete($route['id']);
                },$routes);
            }

            $this->routesDao->delete($id);

            Db::commit();//提交事物
        }catch (\Exception $exception){
            Db::rollback();//回滚事物
            throw new ServiceException($exception->getMessage());
        }
    }

    public function find($id)
    {
        return $this->routesDao->customQuery()->filterById($id)->withRole()->getOne();
    }


}